<?php 
namespace Api\Controller;
use Api\Controller\VerifyController;
class CommonController extends VerifyController{
	public function _initialize(){
		parent::_initialize();
		$token=I("accessToken");
		if(!empty($token)){
			$this->_checkLogin();
		}
		
	}
	
	/**
	 *  登录验证
	 */
	protected function _checkLogin(){
		$token=I("accessToken");
		
		if(empty($token)) {
			$this->_returnJson(array("result"=>0,"info"=>"empty access_token","code"=>109));
		}
		
		S(C("CACHE_MEM"));
		
		$user=D("User")->getTokenUser($token);
		
		if (!$user) {
			$this->_returnJson(array("result"=>0,"info"=>"incorrect access_token","code"=>106));
		}
		$this->token=$token;
		$user_info=json_decode($user,true);
		
		if ($user_info['status']!=1) {
			$this->_returnJson(array("result"=>0,"info"=>"你的账号已在另一台设备中登录了，请注销重新登录。","errorMsg"=>$user_info['errorMsg'],"code"=>107));
		}
		
		if (time()>$user_info['expireTime']) {
			$this->_returnJson(array("result"=>0,"info"=>"access_token timeout","code"=>108));
		}
		
		$this->user=$user_info;
	}
	
	/**
	 *  检测用户账号
	 */
	protected function _checkAccount(){
		$mobileNum=$this->user['mobileNum'];
		$info=D("User")->getUser($mobileNum);
		 
		//不存在账号
		if (!$info) {
			$this->_returnJson(array("result"=>0,"info"=>"account not exist","code"=>"102"));
		}
		 
		//账号被冻结
		if ($info['isOn']==2) {
			$this->_returnJson(array("result"=>0,"info"=>"problem account","errorMsg"=>$info['errorMsg'],"code"=>"104"));
		}
		 
		return $info;
	}
	
	
	public function __destruct(){
		parent::__destruct();
		//最后做接口日志
	}
	
	protected function _returnJson($return){
		if (!$return['result']) {
			$this->_errorCode=$return['code'];
		}
		$this->ajaxReturn($return,"json");
	}
	
	
}
?>